Trò chơi Pac-Man

20.567 lượt xem;
1 using System;
2 using
UnityEngine;
3 using
System.Collections;
4 using
System.Text.RegularExpressions;
5 using
UnityEngine.UI;
6
7 public
class GameGUINavigation : MonoBehaviour {
8
9     
//------------------------------------------------------------------
10     
// Variable declarations
11     
12     
private bool _paused;
13     
private bool quit;
14     
private string _errorMsg;
15     
//public bool initialWaitOver = false;
16
17     
public float initialDelay;
18
19     
// canvas
20     
public Canvas PauseCanvas;
21     
public Canvas QuitCanvas;
22     
public Canvas ReadyCanvas;
23     
public Canvas ScoreCanvas;
24     
public Canvas ErrorCanvas;
25     
public Canvas GameOverCanvas;
26     
27     
// buttons
28     
public Button MenuButton;
29
30     
//------------------------------------------------------------------
31     
// Function Definitions
32
33     
// Use this for initialization
34     
void Start ()
35     {
36         StartCoroutine(
"ShowReadyScreen", initialDelay);
37     }
38     
39     
// Update is called once per frame
40     
void Update ()
41     {
42         
if(Input.GetKeyDown(KeyCode.Escape))
43         {
44             
// if scores are show, go back to main menu
45             
if(GameManager.gameState == GameManager.GameState.Scores)
46                 Menu();
47
48             
// if in game, toggle pause or quit dialogue
49             
else
50             {
51                 
if(quit == true)
52                     ToggleQuit();
53                 
else
54                     TogglePause();
55             }
56         }
57     }
58
59     
// public handle to show ready screen coroutine call
60     
public void H_ShowReadyScreen()
61     {
62         StartCoroutine(
"ShowReadyScreen", initialDelay);
63     }
64
65     
public void H_ShowGameOverScreen()
66     {
67         StartCoroutine(
"ShowGameOverScreen");
68     }
69
70     IEnumerator ShowReadyScreen(
float seconds)
71     {
72         
//initialWaitOver = false;
73         GameManager.gameState = GameManager.GameState.Init;
74         ReadyCanvas.enabled =
true;
75         
yield return new WaitForSeconds(seconds);
76         ReadyCanvas.enabled =
false;
77         GameManager.gameState = GameManager.GameState.Game;
78         
//initialWaitOver = true;
79     }
80
81     IEnumerator ShowGameOverScreen()
82     {
83         Debug.Log(
"Showing GAME OVER Screen");
84         GameOverCanvas.enabled =
true;
85         
yield return new WaitForSeconds(2);
86         Menu();
87     }
88
89     
public void getScoresMenu()
90     {
91         Time.timeScale =
0f; // stop the animations
92         GameManager.gameState = GameManager.GameState.Scores;
93         MenuButton.enabled =
false;
94         ScoreCanvas.enabled =
true;
95     }
96
97     
//------------------------------------------------------------------
98     
// Button functions
99
100     
public void TogglePause()
101     {
102         
// if paused before key stroke, unpause the game
103         
if(_paused)
104         {
105             Time.timeScale =
1;
106             PauseCanvas.enabled =
false;
107             _paused =
false;
108             MenuButton.enabled =
true;
109         }
110         
111         
// if not paused before key stroke, pause the game
112         
else
113         {
114             PauseCanvas.enabled =
true;
115             Time.timeScale =
0.0f;
116             _paused =
true;
117             MenuButton.enabled =
false;
118         }
119
120
121         Debug.Log(
"PauseCanvas enabled: " + PauseCanvas.enabled);
122     }
123     
124     
public void ToggleQuit()
125     {
126         
if(quit)
127         {
128             PauseCanvas.enabled =
true;
129             QuitCanvas.enabled =
false;
130             quit =
false;
131         }
132         
133         
else
134         {
135             QuitCanvas.enabled =
true;
136             PauseCanvas.enabled =
false;
137             quit =
true;
138         }
139     }
140
141     
public void Menu()
142     {
143         Application.LoadLevel(
"menu");
144         Time.timeScale =
1.0f;
145
146         
// take care of game manager
147         GameManager.DestroySelf();
148     }
149
150     IEnumerator AddScore(
string name, int score)
151     {
152         
string privateKey = "pKey";
153         
string AddScoreURL = "http://ilbeyli.byethost18.com/addscore.php?";
154         
string hash = Md5Sum(name + score + privateKey);
155
156         Debug.Log(
"Name: " + name + " Escape: " + WWW.EscapeURL(name));
157
158         WWW ScorePost =
new WWW(AddScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash );
159         
yield return ScorePost;
160
161         
if (ScorePost.error == null)
162         {
163             Debug.Log(
"SCORE POSTED!");
164
165             
// take care of game manager
166             Destroy(GameObject.Find(
"Game Manager"));
167             GameManager.score =
0;
168             GameManager.Level =
0;
169
170             Application.LoadLevel(
"scores");
171             Time.timeScale =
1.0f;
172         }
173         
else
174         {
175             Debug.Log(
"Error posting results: " + ScorePost.error);
176         }
177
178         
yield return new WaitForSeconds(2);
179     }
180
181     
public string Md5Sum(string strToEncrypt)
182     {
183         System.Text.UTF8Encoding ue =
new System.Text.UTF8Encoding();
184         
byte[] bytes = ue.GetBytes(strToEncrypt);
185
186         
// encrypt bytes
187         System.Security.Cryptography.MD5CryptoServiceProvider md5 =
new System.Security.Cryptography.MD5CryptoServiceProvider();
188         
byte[] hashBytes = md5.ComputeHash(bytes);
189
190         
// Convert the encrypted bytes back to a string (base 16)
191         
string hashString = "";
192
193         
for (int i = 0; i < hashBytes.Length; i++)
194         {
195             hashString += System.Convert.ToString(hashBytes[i],
16).PadLeft(2, '0');
196         }
197
198         
return hashString.PadLeft(32, '0');
199     }
200
201     
public void SubmitScores()
202     {
203         
// Check username, post to database if its good to go
204         
int highscore = GameManager.score;
205         
string username = ScoreCanvas.GetComponentInChildren<InputField>().GetComponentsInChildren<Text>()[1].text;
206         Regex regex =
new Regex("^[a-zA-Z0-9]*$");
207
208         
if (username == "") ToggleErrorMsg("Username cannot be empty");
209         
else if (!regex.IsMatch(username)) ToggleErrorMsg("Username can only consist alpha-numberic characters");
210         
else if (username.Length > 10) ToggleErrorMsg("Username cannot be longer than 10 characters");
211         
else StartCoroutine(AddScore(username, highscore));
212         
213     }
214
215     
public void LoadLevel()
216     {
217         GameManager.Level++;
218         Application.LoadLevel(
"game");
219     }
220
221     
public void ToggleErrorMsg(string errorMsg)
222     {
223         
if (ErrorCanvas.enabled)
224         {
225             ScoreCanvas.enabled =
true;
226             ErrorCanvas.enabled =
false;
227
228         }
229         
else
230         {
231             ScoreCanvas.enabled =
false;
232             ErrorCanvas.enabled =
true;
233             ErrorCanvas.GetComponentsInChildren<Text>()[
1].text = errorMsg;
234
235         }
236     }
237 }


------------------------------------------------------------------

Variable declarations

public bool initialWaitOver = false;

canvas

buttons

------------------------------------------------------------------

Function Definitions

Use this for initialization

Update is called once per frame

if scores are show, go back to main menu

if in game, toggle pause or quit dialogue

public handle to show ready screen coroutine call

initialWaitOver = false;

initialWaitOver = true;

Time.timeScale = 0f; stop the animations

------------------------------------------------------------------

Button functions

if paused before key stroke, unpause the game

if not paused before key stroke, pause the game

take care of game manager

string AddScoreURL = "http:ilbeyli.byethost18.comaddscore.php?";

take care of game manager

encrypt bytes

Convert the encrypted bytes back to a string (base 16)

Check username, post to database if its good to go



Gõ tìm kiếm nhanh...